package com.boc.langxiang.ui.fragment.me;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.boc.langxiang.Constant;
import com.boc.langxiang.R;
import com.boc.langxiang.contract.me.MyExerciseWantFragmentContract;
import com.boc.langxiang.entity.response.TobecanyuContentBean;
import com.boc.langxiang.presenter.me.MyExerciseWantFragmentPresenter;
import com.boc.langxiang.ui.activity.ShaloDetailActivity;
import com.boc.langxiang.ui.adapter.me.MyExerciseWantFragmentRVAdapter;
import com.boc.langxiang.ui.adapter.me.SpacesItemDecoration;
import com.boc.langxiang.ui.fragment.BaseFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2016/6/23.
 * 我的活动想加参与的
 */
public class MyExerciseWantFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, MyExerciseWantFragmentContract.View {


    @BindView(R.id.recylerview)
    RecyclerView recylerview;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private MyExerciseWantFragmentRVAdapter mAdapter;
    private boolean mVisible = false;
    private MyExerciseWantFragmentPresenter mPresenter;

    private int mCurrentPage = 1;
    private boolean isRefresh;
    private boolean isLoadMore;

    public static MyExerciseWantFragment getInstance() {
        MyExerciseWantFragment myExerciseWantFragment = new MyExerciseWantFragment();
        Bundle args = new Bundle();
        args.putString(Constant.ME_MY_EXERCISE, "想参与的");
        myExerciseWantFragment.setArguments(args);
        return myExerciseWantFragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = View.inflate(getContext(), R.layout.fragment_me_myexercise_want, null);
        ButterKnife.bind(this, inflate);
        return inflate;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW);
        recylerview.setHasFixedSize(true);
        recylerview.setLayoutManager(new GridLayoutManager(mContext, 1));
        initAdapter();
        mPresenter = new MyExerciseWantFragmentPresenter(this);
        mPresenter.mytobecanyu(String.valueOf(mCurrentPage));
    }

    private void initAdapter() {
        mAdapter = new MyExerciseWantFragmentRVAdapter(mContext, null, this);
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.view_empty, (ViewGroup) recylerview.getParent(), false);
        mAdapter.setEmptyView(emptyView);
        mAdapter.openLoadAnimation();
        mAdapter.isFirstOnly(false);
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.openLoadMore(Constant.PAGE_SIZE, true);
        recylerview.addItemDecoration(new SpacesItemDecoration(mContext, 25));
        recylerview.setAdapter(mAdapter);
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                TobecanyuContentBean item = mAdapter.getItem(position);
                Intent intent = new Intent(mContext, ShaloDetailActivity.class);
                intent.putExtra("id", Integer.valueOf(item.getProduct_id()));
                startActivity(intent);
            }
        });

        recylerview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(isRefresh){
                    return true;
                }
                return false;
            }
        });
    }

    public MyExerciseWantFragmentRVAdapter getmAdapter() {
        return mAdapter;
    }

    public boolean getIconVisible() {
        return mVisible;
    }

    public void setIconVisible(boolean visible) {
        this.mVisible = visible;
    }


    @Override
    public void onRefresh() {
        if (isRefresh || isLoadMore) {
            return;
        }
        isRefresh = true;
        mCurrentPage = 1;

        mPresenter.mytobecanyu(String.valueOf(mCurrentPage));
    }

    @Override
    public void onLoadMoreRequested() {
        if (isRefresh || isLoadMore) {
            return;
        }

        isLoadMore = true;
        mCurrentPage++;
        mPresenter.loadMore(String.valueOf(mCurrentPage));

    }

    @Override
    public void showLoading() {
        if(getUserVisibleHint()){
            showProgressDialog();
        }
    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        isRefresh = false;
        swipeLayout.setRefreshing(false);
        isLoadMore = false;
        mAdapter.notifyDataChangedAfterLoadMore(false);
        if(getUserVisibleHint()){
            Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void getMytobecanyu(List<TobecanyuContentBean> list, int count) {
        swipeLayout.setRefreshing(false);
        isRefresh = false;
        if (list == null) {
            return;
        }
        mAdapter.setNewData(list);
    }

    @Override
    public void getLoadMoreList(List<TobecanyuContentBean> content, int count) {
        if (!isAdded() || getActivity().isFinishing()) {
            return;
        }

        isLoadMore = false;
        if (content == null) {
            mAdapter.notifyDataChangedAfterLoadMore(false);
            loadMoreOver();
            return;
        }
        if (content.size() < Constant.PAGE_SIZE) {
            mAdapter.notifyDataChangedAfterLoadMore(content, false);
            loadMoreOver();
        } else {
            mAdapter.notifyDataChangedAfterLoadMore(content, true);
        }

    }


    /**
     * 点击了删除的图片删除的按钮
     *
     * @param adapterPosition
     * @param id
     */
    @Override
    public void onDeleteIconClick(int adapterPosition, String id) {
        if (mPresenter != null) {
            mPresenter.delMytobecanyu(id, adapterPosition);
        }
    }

    @Override
    public void deleteCanyuSuccess(int adapterPosition) {
//        if (mPresenter != null) {
//            mCurrentPage = 1;
//            mPresenter.mytobecanyu(String.valueOf(mCurrentPage));
//        }
        mAdapter.getData().remove(adapterPosition);
        mAdapter.notifyDataSetChanged();
    }

    private void loadMoreOver() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_load_done, (ViewGroup) recylerview.getParent(), false);
        mAdapter.addFooterView(view);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
